package com.goodving.guli.bt.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;

import com.goodving.guli.bt.profile.scale.GuliLog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by john on 2015/12/7.
 */
public class C1894O {
    private static final String f10590a = "Util";
    private static ProgressDialog f10591b;

    static {
        f10591b = null;
    }

    public static Bitmap m15164a(Context context, String str) {
        InputStream openInputStream = null;
        Throwable th;
        Drawable drawable = null;
        Uri fromFile = Uri.fromFile(new File(str));
        try {
            openInputStream = context.getContentResolver().openInputStream(fromFile);
            try {
                drawable = Drawable.createFromStream(openInputStream, null);
                if (openInputStream != null) {
                    try {
                        openInputStream.close();
                    } catch (IOException e) {
                        GuliLog.tracePrintWarn("ImageView", "Unable to close content: " + fromFile);
                    }
                }
            } catch (Exception e2) {
                try {
                    GuliLog.tracePrintWarn("ImageView", "Unable to open content: " + fromFile);
                    if (openInputStream != null) {
                        try {
                            openInputStream.close();
                        } catch (IOException e3) {
                            GuliLog.tracePrintWarn("ImageView", "Unable to close content: " + fromFile);
                        }
                    }
                    return ((BitmapDrawable) drawable).getBitmap();
                } catch (Throwable th2) {
                    th = th2;
                    if (openInputStream != null) {
                        try {
                            openInputStream.close();
                        } catch (IOException e4) {
                            GuliLog.tracePrintWarn("ImageView", "Unable to close content: " + fromFile);
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            //TBD
        }
        return ((BitmapDrawable) drawable).getBitmap();
    }

    public static Bitmap m15165a(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        int min = Math.min(bitmap.getWidth(), bitmap.getHeight());
        GuliLog.tracePrintInfo(f10590a, "round bitmap w=" + bitmap.getWidth() + ", h=" + bitmap.getHeight());
        Bitmap createBitmap = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        Paint paint = new Paint();
        Rect rect = new Rect();
        RectF rectF = new RectF();
        if (bitmap.getWidth() > bitmap.getHeight()) {
            rect.left = (bitmap.getWidth() - min) / 2;
            rect.top = 0;
            rect.right = bitmap.getWidth() - rect.left;
            rect.bottom = bitmap.getHeight();
        } else if (bitmap.getWidth() < bitmap.getHeight()) {
            rect.left = 0;
            rect.top = (bitmap.getHeight() - min) / 2;
            rect.right = bitmap.getWidth();
            rect.bottom = bitmap.getHeight() - rect.top;
        } else {
            rect.left = 0;
            rect.top = 0;
            rect.right = bitmap.getWidth();
            rect.bottom = bitmap.getHeight();
        }
        rectF.left = 0.0f;
        rectF.top = 0.0f;
        rectF.right = (float) min;
        rectF.bottom = (float) min;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(0xff000000);
        min /= 2;
        canvas.drawRoundRect(rectF, (float) min, (float) min, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rectF, paint);
        return createBitmap;
    }

    public static void m15166a() {
        try {
            GuliLog.tracePrintInfo(f10590a, "mPd = " + f10591b + ", is showing=" + f10591b.isShowing());
            if (f10591b != null) {
                f10591b.dismiss();
                f10591b.hide();
            }
        } catch (Exception e) {
            GuliLog.tracePrintInfo(f10590a, "Exception: mPd = " + f10591b);
        }
    }

    public static void m15167a(Context context, String str, String str2) {
        C1894O.m15168a(context, str, str2, false);
    }

    public static void m15168a(Context context, String str, String str2, boolean z) {
        if (f10591b == null || !f10591b.isShowing()) {
            try {
                f10591b = ProgressDialog.show(context, str, str2);
                f10591b.setCancelable(z);
                return;
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
        C1894O.m15169a(str2);
    }

    public static void m15169a(String str) {
        try {
            if (f10591b != null && f10591b.isShowing()) {
                f10591b.setMessage(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
